package hot_100;

import java.util.Deque;
import java.util.LinkedList;

public class 有效的括号 {
    public static void main(String[] args) {

    }

    public boolean isValid(String s) {
        Deque<Character> deque = new LinkedList<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (!deque.isEmpty()) {
                Character first = deque.getFirst();
                if (first == '(' && chars[i] == ')')
                    deque.removeFirst();
                else if (first == '{' && chars[i] == '}')
                    deque.removeFirst();
                else if (first == '[' && chars[i] == ']')
                    deque.removeFirst();
            } else
                deque.addFirst(chars[i]);
        }
        return deque.isEmpty();
    }
}
